Delete obj

Hello,
This may be a silly question, but I'm having trouble understanding the delete function.
You see, I created the function in which I compare two objects with an attribute with common values and the oldest would be deleted.
But the function doesn't work.
I just want to know if the error is in the delete part. Or if I got that right.

Thanks

 

void delete(Object obj1, obj2)
{
 
m = current Module    
string idStr
 
string auxtag2
auxtag2 = obj2."Old tag" ""
 
for obj1 in m do {
 
idStr = obj1."Old tag" ""
 
if (idStr == auxtag2) {
 
if(obj1."Last Modified On" "" < dateAndTime(today)""){
delete obj1
}
if(obj2."Last Modified On" "" < dateAndTime(today)""){
delete obj2
}
}
    }
}

braolive - Thu Oct 24 14:01:38 EDT 2013

Re: Delete obj
llandale - Thu Oct 24 14:49:05 EDT 2013

Your code has some serious problems nothing to do with "delete(Object)".

[1] It doesn't make sense to accept "obj1" as a parameter, and then use that variable in a loop.

[2] You don't want objects in the "current" module, you want objects in the same module as obj2:
m = module(obj2)

[3] I think you want to compare the "Last Modified On" date of the two objects; comparing it to "today" will almost always result in deleting both objects.

[4] The "for o in m do" loop puts you at the mercy of the current view settings, especially the current Filter.  You probably want:
     for o in entire m do
     {  if (isDeleted(o))   continue

[5] Use "hardDelete(Object)"

[6] flushDeletions() when done.

[7] if (!isEdit(m)) then errorBox("Open module edit")

-Louie

Re: Delete obj
braolive - Fri Oct 25 08:05:13 EDT 2013

llandale - Thu Oct 24 14:49:05 EDT 2013

Your code has some serious problems nothing to do with "delete(Object)".

[1] It doesn't make sense to accept "obj1" as a parameter, and then use that variable in a loop.

[2] You don't want objects in the "current" module, you want objects in the same module as obj2:
m = module(obj2)

[3] I think you want to compare the "Last Modified On" date of the two objects; comparing it to "today" will almost always result in deleting both objects.

[4] The "for o in m do" loop puts you at the mercy of the current view settings, especially the current Filter.  You probably want:
     for o in entire m do
     {  if (isDeleted(o))   continue

[5] Use "hardDelete(Object)"

[6] flushDeletions() when done.

[7] if (!isEdit(m)) then errorBox("Open module edit")

-Louie

I'm really having difficult in this.

What I'm trying to do is look for objects that have the same "old tag" attribute and delete the older one. 

I'm happy to know that the error isn't in the delete part. At least that I got right. But the loop to find the similar ones is where I'm stuck.

 

But thank you.

Re: Delete obj
pommCannelle - Fri Oct 25 08:20:51 EDT 2013

braolive - Fri Oct 25 08:05:13 EDT 2013

I'm really having difficult in this.

What I'm trying to do is look for objects that have the same "old tag" attribute and delete the older one. 

I'm happy to know that the error isn't in the delete part. At least that I got right. But the loop to find the similar ones is where I'm stuck.

 

But thank you.

( Hi !!
euh ... Louie is right, you should modify your code to deal with his remarks ... Then you should think about the management of existing links ... the delete function should return an error if your objects have incoming links ... Feel free to post your code if you are in trouble. o;) )

Re: Delete obj
GregM_dxler - Fri Oct 25 09:01:14 EDT 2013

llandale - Thu Oct 24 14:49:05 EDT 2013

Your code has some serious problems nothing to do with "delete(Object)".

[1] It doesn't make sense to accept "obj1" as a parameter, and then use that variable in a loop.

[2] You don't want objects in the "current" module, you want objects in the same module as obj2:
m = module(obj2)

[3] I think you want to compare the "Last Modified On" date of the two objects; comparing it to "today" will almost always result in deleting both objects.

[4] The "for o in m do" loop puts you at the mercy of the current view settings, especially the current Filter.  You probably want:
     for o in entire m do
     {  if (isDeleted(o))   continue

[5] Use "hardDelete(Object)"

[6] flushDeletions() when done.

[7] if (!isEdit(m)) then errorBox("Open module edit")

-Louie

Hello,

I'm surprised Louie didn't mention to not delete the loop variable obj1 inside the loop.  I think it would work ok if you deleted the object in the loop and then do the flushDeletions after the loop is completed.  The normal practice would be to add it to a skip list and then after the loop is completed, delete the objects in the skip list.

Greg

Re: Delete obj
braolive - Fri Oct 25 09:28:49 EDT 2013

pommCannelle - Fri Oct 25 08:20:51 EDT 2013

( Hi !!
euh ... Louie is right, you should modify your code to deal with his remarks ... Then you should think about the management of existing links ... the delete function should return an error if your objects have incoming links ... Feel free to post your code if you are in trouble. o;) )

If you can help...I appreciate it.


Attachments

copyNEW.dxl

Re: Delete obj
llandale - Mon Oct 28 10:41:57 EDT 2013

braolive - Fri Oct 25 09:28:49 EDT 2013

If you can help...I appreciate it.

I did not run the code.  I'm looking only at function "del()".

I think you want something like this:

  • Skip skpTags = createString() // KEY 'string' Tag; DATA 'Object' with most recent Tag
  • Skip skpDelete = create()   // KEY and DATA both 'Object'
  • for oCurr in entire mTarget do
  • {  if (isDeleted(oCurr))  then continue
  •     OldTag =  oCurr."Old Tag"
  •     if (null OldTag)    then continue
  •     if (oCurr is otherwise disqualified) then continue
  •     if (find(skpTags, OldTag, oPrev))
  •     {       // we have already encountered a previous object with this Tag 
  •        if (LastDate(oCurr) < LastDate(oPrev)) //
  •        then put (skpDelete, oCurr, oCurr)
  •        else {delete oPrev from skpTags; put oCurr in skpDates; put oPrev in skpDelete}
  •    }
  •    else
  •    {        // first encounter of this Tag
  •       put(skpTags, OldTag, oCurr)
  •    }
  • }      // end finding objects to delete
  •        // GregM is right:
  • for oCurr in skpDeletes do
  • {   noError()
  •     hardDelete(oCurr)
  •     ErrMes = lastError()
  •     report ErrMess
  • }
  • flushDeletions()
  • delete the Skips

I have doubts that "Last Modified Date" is a suitable condition.  What if someone just modified the wrong object?

I think you want function lastModifiedTime(Object) instead of o."Last Modified On" (but I've never actually used it)

-Louie

-Louie

Re: Delete obj
pommCannelle - Thu Oct 31 10:17:17 EDT 2013

GregM_dxler - Fri Oct 25 09:01:14 EDT 2013

Hello,

I'm surprised Louie didn't mention to not delete the loop variable obj1 inside the loop.  I think it would work ok if you deleted the object in the loop and then do the flushDeletions after the loop is completed.  The normal practice would be to add it to a skip list and then after the loop is completed, delete the objects in the skip list.

Greg

( well ... the link treatment is missing ... You may have some problem if you try to delete linked objects, and if you don't then you'll have some problems with links without available target.
I'm affraid you have to delete incoming links before the object deletion ... And outcoming links too if you have links to baselined objects ... o;) )

Re: Delete obj
GregM_dxler - Thu Oct 31 10:47:08 EDT 2013

pommCannelle - Thu Oct 31 10:17:17 EDT 2013

( well ... the link treatment is missing ... You may have some problem if you try to delete linked objects, and if you don't then you'll have some problems with links without available target.
I'm affraid you have to delete incoming links before the object deletion ... And outcoming links too if you have links to baselined objects ... o;) )

Yes, pommCannelle your right.  Both outlinks and inlinks need to be deleted before the object is deleted.  Outlinks might not be needed to be deleted, but I've seen some problems if we don't do that, so I just make it a habit.

Of course, in order to delete inlinks, you need to open up the source module in editable mode.

Another thing to worry about is that when an object is deleted, all of it's children get deleted as well.  So not only do you need to check and delete in/out links in the object but also delete in/out links in the children.  Something that bite me in the past.

I'll attach a snippet of code where I do object deleting with checking and deleting in/out links.  It is not workable code, as I just copied the pertainent parts.  Note too, that this does not include the use of skip lists.

Hope this helps,

Greg


Attachments

Del_Obj.dxl